
import java.util.Vector;
import java.util.Scanner;
import java.util.LinkedList;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Alejo
 */
public class main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        // read System.in one character at a time

       
        
        Scanner s = new Scanner(System.in).useDelimiter("");
        System.out.println("Ingrese su cadena porfavor finalice cadena con 1 porfavor");
      

        
        LinkedList<node> freq = new LinkedList<node>();
        // read data from input
      boolean bandera= true;
while (bandera)
{   
           
// s.next() returns string; we're interested in first char
        char c = s.next().charAt(0);    
        
        System.out.println(c);
        if (c== '1')
            bandera = false;
// look up character in frequency list
        node query = new node(c);
        System.out.println("nuevo nodo query "+ query);
       node item = null;
       node temp = null;
        for (int i =0; i < freq.size(); i ++){
            temp = freq.get(i);
            if (temp.equals(query)){
               item = temp;
               freq.remove(i);
               break;
            }
        }
        //node item = freq.remove(query);
        System.out.println("item query "+item);
        if (item == null)
        { // not found, add new node
        freq.addFirst(query);
        System.out.println("se ingreso el query" +query);
        } 
        else 
        { // found, increment node
            item.frequency++;
            freq.addFirst(item);
            System.out.println("se ingreso el item "+item);
        }
        
       System.out.println("linkedlist "+freq);
}
freq.remove(0);
System.out.println("linkedlist "+freq);
 //insert each character into a Huffman tree
PriorityQueue<huffmanTree> trees = new PriorityVector<huffmanTree>();
// merge trees in pairs until one remains
for (node n : freq)
{
trees.add(new huffmanTree(n));
}
while (trees.size() > 1)
{
// grab two smallest values
huffmanTree smallest = (huffmanTree)trees.remove();
huffmanTree small = (huffmanTree)trees.remove();
// add bigger tree containing both
trees.add(new huffmanTree(smallest,small));
}
huffmanTree encoding = trees.remove();
encoding.print();
}
        // TODO code application logic here
    }

